package cn.gemframe.controller;

import cn.gemframe.exception.GemException;
import cn.gemframe.exception.status.GemErrorStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
public class GemBaseController
{

	@ExceptionHandler
	@ResponseBody
	public String handleException(Exception e)
	{
		log.error("--- 拦截到了非检验型异常,异常信息如下: {}",e);

		JSONObject jsonObject = new JSONObject();
		if (e instanceof GemException)
		{
			GemException exception = (GemException) e;
			jsonObject.put("code", exception.getCode());
			jsonObject.put("msg", exception.getMsg());
			jsonObject.put("data", "非检测型异常");
		}
		else
		{
			jsonObject.put("code", GemErrorStatus.SYSTEM_ERROR.getCode());
			jsonObject.put("msg", GemErrorStatus.SYSTEM_ERROR.getMsg());
			jsonObject.put("data", "非检测型异常");
		}
		return JSON.toJSONString(jsonObject);
	}

}
